/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.client.widget;

import java.util.Date;

import com.genia.toolbox.web.gwt.basics.client.Images;
import com.genia.toolbox.web.gwt.basics.client.i18n.I18nCallBack;
import com.genia.toolbox.web.gwt.basics.client.support.GwtObjectUtils;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * a widget that allow the user to select a {@link Date}.
 */
public class DateChooser
    extends Composite
    implements AbstractDateChooser
{
  /**
   * the {@link ChangeListener} called when a date is selected by the user on
   * the floating {@link I18nDatePicker}.
   */
  private final class DateChooserChangeListener
      implements ChangeListener
  {

    /**
     * The {@link Timer} use to remove the {@link DateChooserChangeListener}
     * from the {@link ChangeListener} of the {@link I18nDatePicker}. This has
     * to be done this way, otherwise a concurrent modification exception will
     * occures.
     */
    private final Timer changeListenerRemover = new Timer()
    {
      @Override
      public void run()
      {
        datePicker.removeChangeListener(datePickerChangeListener);
      }
    };



    /**
     * fired when a widget changes, where 'change' is defined by the widget
     * sending the event.
     * 
     * @param sender
     *          the widget that has changed
     * @see com.google.gwt.user.client.ui.ChangeListener#onChange(com.google.gwt.user.client.ui.Widget)
     */
    public void onChange(Widget sender)
    {
      setValue(datePicker.getSelectedDate());
      datePicker.hide();
      changeListenerRemover.schedule(1);
    }
  }

  /**
   * the {@link ClickListener} called when the user clicked on the widget to
   * change the date.
   */
  private final class DateChooserClickListener
      implements ClickListener
  {

    /**
     * the callback method called when the button is pressed.
     * 
     * @param widget
     *          the button being pressed
     * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
     */
    public void onClick(Widget widget)
    {
      if (!isEnabled()) {
        return;
      }
      datePicker.setSelectedDate(getValue());
      datePicker.addChangeListener(datePickerChangeListener);
      datePicker.show(dateLabel);
    }
  }

  /**
   * the format of the displayed date.
   */
  private static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy";

  /**
   * the registered {@link ChangeListener}s.
   */
  private ChangeListenerCollection changeListeners = null;

  /**
   * the date format.
   */
  private String dateFormat = DEFAULT_DATE_FORMAT;

  /**
   * the label displaying the current selected date.
   */
  private final Label dateLabel = new Label();

  /**
   * the {@link I18nDatePicker} used to let the user select the date.
   */
  private final I18nDatePicker datePicker = I18nDatePicker.getFloatingDatePicker();

  /**
   * the {@link DateChooserChangeListener} used to communicate with the
   * {@link I18nDatePicker}.
   */
  private final DateChooserChangeListener datePickerChangeListener = new DateChooserChangeListener();

  /**
   * whether the widget is enable.
   */
  private boolean enableState = true;

  /**
   * the panel containing this widget.
   */
  private final HorizontalPanel panel = new HorizontalPanel();

  /**
   * the current value of this widget.
   */
  private Date value;



  /**
   * constructor.
   */
  public DateChooser()
  {
    Image calendarImage = Images.IMAGES.gwtcCalendar().createImage();
    panel.add(dateLabel);
    panel.add(calendarImage);
    setValue(new Date());
    DateChooserClickListener dateChooserClickListener = new DateChooserClickListener();
    calendarImage.addClickListener(dateChooserClickListener);
    dateLabel.addClickListener(dateChooserClickListener);
    initWidget(panel);
  }



  /**
   * register a new {@link ChangeListener}.
   * 
   * @param listener
   *          the {@link ChangeListener} to register
   */
  public void addChangeListener(ChangeListener listener)
  {
    if (changeListeners == null)
      changeListeners = new ChangeListenerCollection();
    changeListeners.add(listener);
  }



  /**
   * getter for the dateFormat property.
   * 
   * @return the dateFormat
   */
  public String getDateFormat()
  {
    return dateFormat;
  }



  /**
   * getter for the value property.
   * 
   * @return the value
   */
  public Date getValue()
  {
    return value;
  }



  /**
   * getter for the enableState property.
   * 
   * @return the enableState
   */
  public boolean isEnabled()
  {
    return enableState;
  }



  /**
   * remove a registered {@link ChangeListener}.
   * 
   * @param listener
   *          the {@link ChangeListener} to remove
   * @see com.google.gwt.user.client.ui.SourcesChangeEvents#removeChangeListener(com.google.gwt.user.client.ui.ChangeListener)
   */
  public void removeChangeListener(ChangeListener listener)
  {
    if (changeListeners != null)
      changeListeners.remove(listener);
  }



  /**
   * setter for the dateFormat property.
   * 
   * @param dateFormat
   *          the dateFormat to set
   */
  public void setDateFormat(String dateFormat)
  {
    this.dateFormat = dateFormat == null ? DEFAULT_DATE_FORMAT : dateFormat;
  }



  /**
   * setter for the enableState property.
   * 
   * @param enableState
   *          the enableState to set
   */
  public void setEnabled(boolean enableState)
  {
    this.enableState = enableState;
  }



  /**
   * setter for the value property.
   * 
   * @param value
   *          the value to set
   */
  public void setValue(Date value)
  {
    if (GwtObjectUtils.nullSafeEquals(this.value, value)) {
      return;
    }
    if (value == null) {
      value = new Date();
    }
    this.value = value;
    final Date currentValue = value;
    I18nDatePicker.formatDate(getDateFormat(), value, new I18nCallBack()
    {

      public void getMessage(String message)
      {
        if (!GwtObjectUtils.nullSafeEquals(getValue(), currentValue)) {
          return;
        }
        dateLabel.setText(message);
        if (changeListeners != null) {
          changeListeners.fireChange(DateChooser.this);
        }
      }

    });
  }

}